package com.jh.user.service;

import cn.jh.common.exception.ServiceException;
import com.jh.user.pojo.enums.ChannelMerType;
import com.jh.user.pojo.enums.PayType;

import java.math.BigDecimal;

/**
 * @author Yichuan
 * @version 1.0
 */
public class UserChannelRequest {

    private Long userId;
    private Long channelId;
    private Short type;
    private BigDecimal rate;
    private BigDecimal extraFee;
    private BigDecimal gradeRate;

    private transient PayType payType;

    private String merType;
    private transient ChannelMerType channelMerType;
    private String channelNo;

    public String getChannelNo() {
        return channelNo;
    }

    public void setChannelNo(String channelNo) {
        this.channelNo = channelNo;
    }

    public Long getUserId() {
        return userId;
    }

    public void setUserId(Long userId) {
        this.userId = userId;
    }

    public Long getChannelId() {
        return channelId;
    }

    public void setChannelId(Long channelId) {
        this.channelId = channelId;
    }

    public Short getType() {
        return type;
    }

    public void setType(Short type) {
        this.type = type;
    }

    public BigDecimal getRate() {
        return rate;
    }

    public void setRate(BigDecimal rate) {
        this.rate = rate;
    }

    public BigDecimal getExtraFee() {
        return extraFee;
    }

    public void setExtraFee(BigDecimal extraFee) {
        this.extraFee = extraFee;
    }

    public BigDecimal getGradeRate() {
        return gradeRate;
    }

    public void setGradeRate(BigDecimal gradeRate) {
        this.gradeRate = gradeRate;
    }

    public PayType getPayType() {
        return payType;
    }

    public void setPayType(PayType payType) {
        this.payType = payType;
    }

    public String getMerType() {
        return merType;
    }

    public void setMerType(String merType) {
        this.merType = merType;
    }

    public ChannelMerType getChannelMerType() {
        return channelMerType;
    }

    public void setChannelMerType(ChannelMerType channelMerType) {
        this.channelMerType = channelMerType;
    }

    public void validation() {
        if (getUserId() == null) {
            throw new ServiceException("用户id不能为空");
        }
        if (getChannelId() == null) {
            throw new ServiceException("通道id不能为空");
        }
        if (getType() == null) {
            throw new ServiceException("交易类型不能为空");
        }
        if (getRate() == null && getExtraFee() == null && getGradeRate() == null) {
            throw new ServiceException("请问您到底要修改什么");
        }
        PayType payType = PayType.of(getType());
        if (payType == null) {
            throw new ServiceException("交易类型异常");
        }
        setPayType(payType);
        if (getRate() != null && getRate().compareTo(BigDecimal.ZERO) < 0) {
            throw new ServiceException("您设置的"+payType.getDesc()+"费率不能为负数");
        }
        if (getExtraFee() != null) {
            if (getExtraFee().compareTo(BigDecimal.ZERO) < 0) {
                throw new ServiceException("您设置的" + payType.getDesc() + "手续费不能为负数");
            }
            if (getExtraFee().compareTo(BigDecimal.ONE) < 0) {
                throw new ServiceException("您设置的" + payType.getDesc() + "手续费不能小于1");
            }
        }
        if (getGradeRate() != null && getGradeRate().compareTo(BigDecimal.ZERO) < 0) {
            throw new ServiceException("您设置的"+payType.getDesc()+"推荐人分润费率不能为负数");
        }
        if (getChannelNo() == null) {
            throw new ServiceException("通道no不能为空");
        }
//        if (getMerType() == null) {
//            throw new ServiceException("通道商户类型不能为空");
//        }
//        ChannelMerType channelMerType = ChannelMerType.of(getMerType());
//        if (channelMerType == null) {
//            throw new ServiceException("通道商户类型异常");
//        }
//        setChannelMerType(channelMerType);
    }
}
